using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;

using IGlobalist.Cms.Model;

using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class CategoryAddEdit : AdminBaseUserControl
    {
        //protected string xmlPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Admin/XML/Categories.xml");
        public event EventHandler EventSaveSuccess;
        public event EventHandler ViewContent;        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.ContentCategory);
                if (cList != null)
                {
                    ddlClassName.DataSource=cList;
                    ddlClassName.DataBind();
                    Newcategory(); 
                }
            }
        }   
  
        public void DoNewCategory(object sender, EventArgs e)
        {

            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
            if (clazz != null)
            {
                ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            }
            Newcategory();
        }

        protected void Newcategory()
        {
            try
            {
                lblStatus.Text = "Add new category";                
                fsContentList.Visible = false;
              
                ddlClassName.Enabled = true;
                ViewState["Category"] = null;
                tbName.Text = "";
                tbName.Focus();
                tbDescription.Text = "";
                tbOrder.Text = "0";
                if (ddlClassName.SelectedIndex == -1)
                    lbError.Text = "Categories Class is null";
                else
                    lbError.Text= "";  
                tbName.ReadOnly = false;
                hdfCategoryID.Value = "0";
                ddlParent.DataBind();
                ddlParent.Enabled = true;
                tbOrder.Enabled = true;
                              
                ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
                ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
                
                if (ucCmsExtPropertiesForm.PropertyList.Count > 4)
                    fsExt.Visible = true;
                else fsExt.Visible = false;
                ViewState["type"]= "cms";                
                ViewState["IDEdu"] = null;

            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);
            }

        }

        public void DoEditCategory(object sender, EventArgs e)
        {
            ddlPage.DataBind();
            ViewState["type"] = "cms";
            try
            {
                ddlParent.Visible = true;
                ddlParentXml.Visible = false;
                CategoryTree catList = (CategoryTree)sender;
                IContentCategory category = CategoryController.GetCategory(Convert.ToInt64(catList.SelectedValue));
                category.ContentList = ContentController.GetContentListLelateToObjectID(category.ID,CmsObjectType.ContentCategory);
                lblStatus.Text = "View Category";
                if (category != null)
                {                    
                    ddlClassName.Enabled= false;
                    fsContentList.Visible = true;   
                    lbtnContent.Text = String.Format("Content list {0} ", category.ContentList.Count);                    
                    ViewState["ContentList"] = category.ContentList;
                    ViewState["Category"] = category;
                    ViewState["CategoryID"] = category.ID;
                    ddlClassName.SelectedValue = category.ClassID.ToString();
                    tbName.Text = category.Name;
                    tbName.ReadOnly = true;
                    tbDescription.Text = category.Description;
                    tbDescription.Focus();
                    hdfCategoryID.Value = category.ID.ToString();
                    ddlParent.DataBind();
                    ddlParent.SelectedValue = category.ParentID.ToString();
                    tbOrder.Text = category.Order.ToString();
                    ucCmsExtPropertiesForm.PropertyList = category.ExtPropertyList;
                    try
                    {
                        ddlPage.SelectedValue = category.FirstVersionID.ToString();
                    }
                    catch
                    { }

                    if (ucCmsExtPropertiesForm.PropertyList.Count > 4)
                        fsExt.Visible = true;
                    else fsExt.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);
            }
        }
 

        public void DoSaveCategory(object sender, EventArgs e)
        {
            try
            {
                bool success = false;
                
                    IContentCategory category = new Category();
                    if (ViewState["Category"] != null)
                    {
                        category = (Category)ViewState["Category"];
                        category.EditedBy = IGlobalistMembership.GetUser().ID;
                    }
                    category.ExtPropertyList = ucCmsExtPropertiesForm.PropertyList;
                    category.Class = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
                    category.ClassID = Convert.ToInt64(ddlClassName.SelectedValue);
                    category.ClassTypeID = Convert.ToInt16(CmsObjectType.ContentCategory);
                    category.Name = tbName.Text;
                    category.ParentID = Convert.ToInt64(ddlParent.SelectedValue);
                    category.Order = Convert.ToInt16(tbOrder.Text);
                    category.Description = tbDescription.Text;
                    category.Author = IGlobalistMembership.GetUser().ID;
                    category.FirstVersionID = Convert.ToInt64(ddlPage.SelectedValue);

                    if (ViewState["Category"] != null)
                    {
                        CategoryController.UpdateCategory(category);
                        success = true;
                        lblStatus.Text = "Update successful";
                    }
                    else
                    {
                        CategoryController.InsertCategory(category);
                        success = true;
                        lblStatus.Text = "Add new successful";
                    }
                

                if ((EventSaveSuccess != null) && (success == true))
                {
                    EventSaveSuccess(this, new EventArgs());
                }
                else
                {
                    tbName.Focus();
                }
                

            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);  
            }
        }              

        public void DoCopyCategory(object sender, EventArgs e)
        {
            tbName.ReadOnly = false;  
            tbName.Text = "";
            ViewState["Category"] = null;
        }

        public void DoBackCategory(object sender, EventArgs e)
        {


        }
              
        
        public bool IsExist(string name)
        {
            IList<IContentCategory> list = CategoryController.GetCategoryList(Convert.ToInt64(ddlClassName.SelectedValue));
            foreach (Category c in list)
            {
                if (c.Name == name)
                    return false;
            }
            return true;           
        }       


        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;            
        }

        protected void ddlClassName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Newcategory();
        }

        #region property
        public string CategoryID
        {
            get { return Convert.ToString(ViewState["CategoryID"]); }
            set { ViewState["CategoryID"] = value; }
        }
        public ContentList Contents
        {
            get { return (ContentList)ViewState["ContentList"]; }
            set { ViewState["ContentList"] = value; }
        }
        #endregion

        protected void lbtnContent_Click(object sender, EventArgs e)
        {
            if (ViewContent != null)
            {
                ViewContent(this, new EventArgs());
            }
        }
    }   
}
